<div class="layui-fluid">
    <div class="layui-card">
        <div class="layui-form layui-card-header layuiadmin-card-header-auto">
            <div class="layui-form-item">
                <div class="layui-inline">
                    <label class="layui-form-label">店铺名称</label>
                    <div class="layui-input-block">
                        <input type="text" name="title" id="title" placeholder="请输入店铺名称" autocomplete="off" class="layui-input">
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">所属区域</label>
                    <div class="layui-input-block">
                        <select name="district" id="district" lay-search="">
                            <option value="">请选择所属区域</option>
                            {foreach $district as $key=>$vo}
                            <option value="{$vo.district}">{$vo.district}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <label class="layui-form-label">所属商圈</label>
                    <div class="layui-input-block">
                        <select name="businessDistrict" id="businessDistrict" lay-search="">
                            <option value="">请选择所属区域</option>
                            <option value="无商圈商家">无商圈商家</option>
                            {foreach $businessDistrict as $key=>$vo}
                            <option value="{$vo.businessDistrict}">{$vo.businessDistrict}</option>
                            {/foreach}
                        </select>
                    </div>
                </div>
                <div class="layui-inline">
                    <button class="layui-btn layuiadmin-btn-admin" lay-submit lay-filter="LAY-user-back-search">
                        <i class="layui-icon layui-icon-search layuiadmin-button-btn"></i>
                    </button>
                </div>
            </div>
        </div>
        <div class="layui-card-body">
            <div style="padding-bottom: 10px;">
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm" title="刷新" data-type="refresh"><i
                        class="layui-icon layui-icon-refresh-3"></i>
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('query/add')?$auth->check('query/add'):'hide'}" title="添加" data-type="add"><i
                        class="layui-icon layui-icon-add-1"></i>添加
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('query/del')?$auth->check('query/del'):'hide'}" title="删除" data-type="batchdel"><i
                        class="layui-icon layui-icon-delete"></i>删除
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('query/export')?$auth->check('query/export'):'hide'}" title="导出" data-type="export"><i
                        class="iconfont icon-daochu"></i>导出
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('query/distribution')?$auth->check('query/distribution'):'hide'}" title="海亮点" data-type="distribution"><i
                        class="iconfont icon-daochu"></i>采集分布海亮点
                </button>
                <button class="layui-btn layuiadmin-btn-admin layui-btn-sm {$auth->check('query/synchronization')?$auth->check('query/synchronization'):'hide'}" title="同步采集数据" style="float: right"
                        data-type="tongbu"><i
                        class="layui-icon layui-icon-delete"></i>同步采集数据
                </button>
            </div>
            <table id="LAY-user-back-manage" lay-filter="LAY-user-back-manage"></table>
            <script type="text/html" id="table-useradmin-admin">
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('query/read')?$auth->check('query/read'):'hide'}"  lay-event="read"><i
                        class="layui-icon layui-icon-read"></i>详情</a>
                <a class="layui-btn layui-btn-normal layui-btn-xs {$auth->check('query/edit')?$auth->check('query/edit'):'hide'}"  lay-event="edit"><i
                        class="layui-icon layui-icon-edit"></i>编辑</a>
                <a class="layui-btn layui-btn-danger layui-btn-xs {$auth->check('query/del')?$auth->check('query/del'):'hide'}" lay-event="del"><i
                        class="layui-icon layui-icon-delete"></i>删除</a>

            </script>
        </div>
    </div>
</div>
<script src="/static/layuiadmin/layui/layui.js"></script>
<script>
    var listUrl = "{:url('query/index')}", create = "{:url('query/add')}", edit = "{:url('query/edit')}",read = "{:url('query/read')}",
        deleteUrl = "{:url('query/del')}",synchronization = "{:url('query/synchronization')}",downloadReportExportUrl = "{:url('query/export')}",distribution = "{:url('query/distribution')}";
    layui.config({
        base: '/static/layuiadmin/' //静态资源所在路径
    }).extend({
        index: 'lib/index' //主入口模块
    }).use(['index', 'table','soulTable'], function () {
        var $ = layui.$
            , form = layui.form
            , table = layui.table, soulTable = layui.soulTable;

        table.render({
            elem: "#LAY-user-back-manage", url: listUrl
            , cellMinWidth: 100
            , overflow: {
                type: 'tips'
                // ,hoverTime: 500 // 悬停时间，单位ms, 悬停 hoverTime 后才会显示，默认为 0
                , color: 'white' // 字体颜色
                , bgColor: '#009688' // 背景色
            }
            , cols: [
                [
                    {type: "checkbox", fixed: "left"},
                    {field: "district", align: "center", width:"200", title: "所属区域"},
                    {field: "title", align: "center",  width:"200",title: "店名"},
                    {field: "isChainStore", align: "center",  width:"200",title: "直营&加盟"},
                    {field: "businessDistrict", align: "center",  width:"200",title: "商圈"},
                    {field: "catPathKey", align: "center",  width:"200",title: "类型"},
                    {field: "address", align: "center",  width:"200",title: "地址"},
                    {field: "telephones", align: "center",  width:"200",title: "电话"},
                    {field: "geoPoint", align: "center",  width:"200",title: "经纬度"},
                    {field: "price", align: "center",  width:"200",title: "平均价格"},
                    {field: "createDate", align: "center", width:"200", title: "采集时间"},
                    {field: "openingHours", align: "center", width:"200", title: "营业时间"},
                    {field: "description", align: "center", width:"200", title: "详情"},
                    {title: "操作", align: "center", width:"300", fixed: "right", toolbar: "#table-useradmin-admin"}]]
            , done: function () {
                soulTable.render(this)
            },
            response: {
                statusCode: 200 //重新规定成功的状态码为 200，table 组件默认为 0
            },
            parseData: function (res) { //将原始数据解析成 table 组件所规定的数据
                 $("#demoReload").val(res.pageToken);
                return {
                    "code": res.code,
                    "count": res.total, //解析数据长度
                    "data": res.data, //解析数据列表
                    "pageToken": res.pageToken
                };

            }
            ,id: 'testReload',
            text: {
                'none': "呀！一条数据都没有？"
            },
            page: true,
            limit: 20,
        });

        //监听行单击事件（双击事件为：rowDouble）
        table.on('rowDouble(LAY-user-back-manage)', function(obj){
            var data = obj.data;
            var param = $.param({'id': data.id});
            layer.open({
                type: 2
                , skin: 'layui-layer-molv'
                , title: '详情读取'
                , content: read + '?' + param
                , area: ['80%', '80%']
                , maxmin: true
            });

        });
        //按钮触发
        table.on("tool(LAY-user-back-manage)", function (e) {
            var param = $.param({'id': e.data.id});
            if ("edit" === e.event) {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '编辑'
                    , content: edit + '?' + param
                    , area: ['80%', '80%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: edit + '?' + param,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    console.log(res);
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('testReload');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            } else if ("del" === e.event) {
                var params = $.param({'ids': e.data.id});
                layer.confirm("确定删除吗？", function (t) {
                    $.ajax({
                        url: deleteUrl + '?' + params, success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('testReload');
                                layer.close(index); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                })
            }else if ("read" === e.event){
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '详情读取'
                    , content: read + '?' + param
                    , area: ['80%', '80%']
                    , maxmin: true
                });
            }
        });
        //监听搜索
        form.on('submit(LAY-user-back-search)', function (data) {
            var field = data.field;
            //执行重载
            table.reload('testReload', {
                where: field
            });
        });

        //事件
        var active = {
            batchdel: function () {
                var checkStatus = table.checkStatus('testReload')
                    , checkData = checkStatus.data; //得到选中的数据
                if (checkData.length === 0) {
                    return layer.msg('请选择数据');
                }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var ids = $.param({'ids': id});
                layer.confirm('确定删除吗？', function (index) {
                    //执行 Ajax 后重载
                    $.ajax({
                        url: deleteUrl,
                        type: "post",
                        data: ids,
                        success: function (res) {
                            if (res.code === 200) {
                                layer.msg(res.msg, {icon: 1});
                                table.reload('testReload');
                                layer.close(index); //关闭弹层
                            } else {
                                console.log(res);
                                layer.msg(res.msg, {icon: 5});
                                return false;
                            }
                        }
                    });
                    // table.reload('LAY-user-back-manage');
                    // layer.msg('已删除');
                });
            }
            , add: function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '添加'
                    , content: create
                    , area: ['80%', '80%']
                    , maxmin: true
                    , btn: ['确定', '取消']
                    , yes: function (index, layero) {
                        var iframeWindow = window['layui-layer-iframe' + index]
                            , submitID = 'LAY-user-back-submit'
                            , submit = layero.find('iframe').contents().find('#' + submitID);
                        //监听提交
                        iframeWindow.layui.form.on('submit(' + submitID + ')', function (data) {
                            var field = data.field; //获取提交的字段
                            //提交 Ajax 成功后，静态更新表格中的数据
                            $.ajax({
                                url: create,
                                type: 'post',
                                data: field,
                                success: function (res) {
                                    if (res.code === 200) {
                                        layer.msg(res.msg, {icon: 1});
                                        table.reload('testReload');
                                        layer.close(index); //关闭弹层
                                    } else {
                                        console.log(res);
                                        layer.msg(res.msg, {icon: 5});
                                        return false;
                                    }
                                }
                            });
                        });
                        submit.trigger('click');
                    }
                });
            }, refresh: function () {
                table.reload('testReload');
            },
            tongbu: function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '同步数据'
                    , content: synchronization
                    , area: ['80%', '80%']
                    , maxmin: true
                })
            },
            export:function () {
                var checkStatus = table.checkStatus('testReload')
                    , checkData = checkStatus.data; //得到选中的数据

                // if (checkData.length === 0 && district === "") {
                //     return layer.msg('请选择需要导出数据');
                // }
                var id = ''; //task_id
                for (var i = 0; i < checkData.length; i++) {
                    id += checkData[i].id + ",";
                }
                id = id.substring(0, id.length - 1);
                var title = $('#title').val(); //店面名称
                var district = document.getElementById('district').value; //区域
                var businessDistrict = document.getElementById('businessDistrict').value; //商圈
                layer.confirm("确定导出数据吗", function (t) {
                    $.ajax({
                        url: downloadReportExportUrl,
                        type: "post",
                        data: {
                            ids: id,
                            title: title,
                            district: district,
                            businessDistrict: businessDistrict,
                        },
                        beforeSend: function () {
                            layer.close(t);
                            layer.load(2, {
                                shade: [0.6, '#fff'], content: '数据下载中...', success: function (layero) {
                                    layero.find('.layui-layer-content').css({
                                        'padding-top': '6px',
                                        'width': '150px',
                                        'padding-left': '40px'
                                    });
                                    layero.find('.layui-layer-ico16, .layui-layer-loading .layui-layer-loading2').css({
                                        'width': '150px !important',
                                        'background-position': '2px 0 !important'
                                    });
                                }
                            });
                        },
                        success: function (res) {
                            if (res.code === 400) {
                                layer.msg(res.message, {icon: 5}, function () {
                                    layer.closeAll();
                                });
                                return false;
                            } else {
                                var dataObj = eval("(" + res + ")");
                                if (dataObj.code == 200) {
                                    layer.closeAll();
                                    window.location.href = dataObj.url;
                                } else {
                                    layer.msg("暂无查询到有效数据", {icon: 5});
                                    return false;
                                }
                            }
                        }
                    });
                });
            },
            distribution:function () {
                layer.open({
                    type: 2
                    , skin: 'layui-layer-molv'
                    , title: '海亮点分布'
                    , content: distribution
                    , area: ['80%', '80%']
                    , maxmin: true
                })
            }
        };
        $('.layui-btn.layuiadmin-btn-admin').on('click', function () {
            var type = $(this).data('type');
            active[type] ? active[type].call(this) : '';
        });
    });
</script>